//
//  PlayerMenuTableViewController.m
//  MountEverestFM
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 chenn. All rights reserved.
//
#import <CoreImage/CoreImage.h>
#import "PlayerMenuTableViewController.h"
#import "Player.h"
#import <UIImage+GIF.h>
#import "MusicModel.h"
#import "PlayerModel.h"

@interface PlayerMenuTableViewController ()

@property (nonatomic, strong) MusicModel *musicModel;
@property (nonatomic, strong) PlayerModel *playModel;

@end

@implementation PlayerMenuTableViewController

- (void)viewWillAppear:(BOOL)animated
{
    self.tabBarController.tabBar.hidden = YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    Player *player = [Player shareInstance];
    self.playModel = player.playerModel;
    self.musicModel = [self.playModel.musicArray objectAtIndex:self.playModel.index];
    // Uncomment the following line to preserve selection between presentations.
    // self.clearsSelectionOnViewWillAppear = NO;
    
    // Uncomment the following line to display an Edit button in the navigation bar for this view controller.
    // self.navigationItem.rightBarButtonItem = self.editButtonItem;
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"list"];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    Player *player = [Player shareInstance];
    if (player.history == NO) {
        return self.playModel.musicArray.count;
    } else {
        return player.historyArr.count;
    }
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"list" forIndexPath:indexPath];
    Player *player = [Player shareInstance];
    if (player.history == NO) {
        MusicModel *musicList = [self.playModel.musicArray objectAtIndex:indexPath.row];
        cell.textLabel.text = musicList.title;
        if (indexPath.row != self.playModel.index) {
            cell.imageView.image = [UIImage imageNamed:@"sound_playbtn@2x.png"];
        } else {
            cell.imageView.image = [UIImage sd_animatedGIFNamed:@"sound_playingbtn.gif"];
        }
    } else {
        HistoryModel *historyModel = [player.historyArr objectAtIndex:indexPath.row];
        cell.textLabel.text = historyModel.title;
        NSLog(@"%@", historyModel.title);
        if (indexPath.row != player.number) {
            cell.imageView.image = [UIImage imageNamed:@"sound_playbtn@2x.png"];
        } else {
            cell.imageView.image = [UIImage sd_animatedGIFNamed:@"sound_playingbtn.gif"];
        }

    }
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    Player *player = [Player shareInstance];
    if (player.playerModel.musicArray.count != 0) {
        [player collectPreseve];
    }
    if (player.history == NO) {
        player.playerModel.index = indexPath.row;
    } else {
        player.number = indexPath.row;
    }
    [player playFromNet];
    [self.navigationController popViewControllerAnimated:YES];
}

@end
